package com.cskaoyan.service;

import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.StatOrderVo;
import com.cskaoyan.bean.vo.StatVoYJ;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author Ramsey
 * @date 2023年05月30日 13:11
 */
public class StatServiceimpl implements StatService {
    @Override
    public List<Map> statUser() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketUserMapper mapper = sqlSession.getMapper(MarketUserMapper.class);
        List<Map> maps = mapper.resultByMap();
        return maps;
    }

    // @Override 2023年6月1日20:25:39改成永健的
    // public List<Map> statOrder() {
    //     SqlSession sqlSession = MyBatisUtil.getSqlSession();
    //     MarketUserMapper mapper = sqlSession.getMapper(MarketUserMapper.class);
    //     List<Map> maps = mapper.OrderresultByMap();
    //     return maps;
    // }


    @Override
    public BaseRespVo statOrder() {
        String[] columns = new String[]{"day", "orders", "customers", "amount", "pcr"};

        // 从数据库读取配置信息，返回给浏览器
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketOrderMapper orderMapper = sqlSession.getMapper(MarketOrderMapper.class);
        // 按日期分组，获得所有的订单
        List<StatOrderVo> statOrderVos = orderMapper.orderGroupByDate();
        // 遍历计算用户的人均消费
        for (StatOrderVo statOrderVo : statOrderVos) {
            Integer userNumber = statOrderVo.getCustomers();
            BigDecimal orderAmount = statOrderVo.getAmount();
            BigDecimal userConsume = orderAmount.divide(new BigDecimal(userNumber));
            statOrderVo.setPcr(userConsume);
        }
        // 关闭sqlSession
        sqlSession.close();

        // 构造需要返回的对象
        StatVoYJ statVo = new StatVoYJ();
        statVo.setColumns(columns);
        statVo.setRows(statOrderVos);

        return BaseRespVo.ok(statVo);
    }

    @Override
    public List<Map> statGoods() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketUserMapper mapper = sqlSession.getMapper(MarketUserMapper.class);
        List<Map> maps = mapper.GoodsresultByMap();
        return maps;
    }
}
